  var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sTime: '',
    eTime: '',
    nowTime: '',
    index: 0,
    historyData: '',
    ids: '',
    pageNum: 1,
    paseSize: 15,
    path: '',
    device_id:0,
    group_id:0,
  },
  bindETime: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      eTime: e.detail.value,
      pageNum: 1,
    })
  },

  bindSTime: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sTime: e.detail.value,
      pageNum: 1,
    })
  },

  showHistory: function (e) {
    var id = e.currentTarget.id;
    for(var i = 0; i<this.data.historyData.length; i++)
      if(id == this.data.historyData[i].time)
        var data = this.data.historyData[i];
      wx.navigateTo({
        url: '../detail/detail?data=' + JSON.stringify(data),
      });
  },

  formSubmit: function (e){
    var This = this;
    wx.showLoading({
      title: '玩命加载中',
    });
    This.setData({
      historyData: []
    });
    wx.getStorage({
      key: 'project_id',
      success: function (succ) {
        wx: wx.request({
          url: getApp().globalurl + '/record/historyData?type=2&sTime=' + This.data.sTime + '&eTime=' + This.data.eTime + '&device_id=' + This.data.device_id + '&group_id=' + This.data.group_id + '&pageNum=' + 1 + '&pageSize=' + This.data.paseSize + '&order=desc',
          data: '',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': succ.data[0],
            'Authorization': succ.data[1]
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            wx.hideLoading();
            if (res.data.code == 200) {
              This.setData({
                historyData: res.data.data,
                pageNum: 2,
              });
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000  
            });
            This.setData({
              historyData: []
            });
          },
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '操作失败，请稍后再试',
          icon: 'none',
          duration: 2000
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data = JSON.parse(options.data);
    var This = this;
    This.setData({
      device_id:data[0]['device_id'],
      group_id:data[0]['group_id']
    });
    var time = util.formatDate(new Date);
    var month = util.formatMonth(new Date);
    This.setData({
      eTime: time,
      sTime: month,
      nowTime: time,
    });
    wx.getStorage({
      key: 'project_id',
      success: function(succ) {
        wx: wx.request({
          url: getApp().globalurl + '/record/historyData?type=2&sTime=' + This.data.sTime + '&eTime=' + This.data.eTime + '&device_id=' + This.data.device_id + '&group_id=' + This.data.group_id + '&pageNum=' + This.data.pageNum + '&pageSize=' + This.data.paseSize + '&order=desc',
          data: '',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': succ.data[0],
            'Authorization': succ.data[1]
            },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            if(res.data.code==200)
            {
              This.setData({
                historyData: res.data.data,
                pageNum:This.data.pageNum+1,
              });
            }else{
              wx.showToast({
                title: '操作失败，请稍后再试',
                icon: 'none',
                duration: 2000
              })
            }
           },
          fail: function (res) {
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            })
           },
        });
      },
      fail: function(res) {
        wx.showToast({
          title: '操作失败，请稍后再试',
          icon: 'none',
          duration: 2000
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var This = this;
    wx.getStorage({
      key: 'project_id',
      success: function(succ) {
        wx: wx.request({
          url: getApp().globalurl + '/record/historyData?type=2&sTime=' + This.data.sTime + '&eTime=' + This.data.eTime + '&device_id=' + This.data.device_id + '&group_id=' + This.data.group_id + '&pageNum=' + 1 + '&pageSize=' + This.data.paseSize + '&order=desc',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': succ.data[0],
            'Authorization': succ.data[1]
            },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            wx.hideNavigationBarLoading();//隐藏导航栏加载
            wx.stopPullDownRefresh();//停止下拉刷新
            if (res.data.code == 200) {
              wx.showToast({
                title: '加载成功',
                icon: 'success',
                duration: 2000
              });
              This.setData({
                historyData: res.data.data,
                pageNum: 2,
              });
            } else {
              wx.showToast({
                title: '操作失败，请稍后再试',
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail: function (res) {
            wx.hideNavigationBarLoading();//隐藏导航栏加载
            wx.stopPullDownRefresh();//停止下拉刷新
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            });
            This.setData({
              historyData: []
            });
          },
        })
      },
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    });
    var This = this;
    wx.getStorage({
      key: 'project_id',
      success: function (succ) {
        wx: wx.request({
          url: getApp().globalurl + '/record/historyData?type=2&sTime=' + This.data.sTime + '&eTime=' + This.data.eTime + '&device_id=' + This.data.device_id + '&group_id=' + This.data.group_id + '&pageNum=' + This.data.pageNum + '&pageSize=' + This.data.paseSize + '&order=desc',
          data: '',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': succ.data[0],
            'Authorization': succ.data[1]
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            wx.hideLoading();
            if (res.data.data == null) {
              wx.showToast({
                title: '到底了',
                icon: 'none',
                duration: 2000
              });
            } else {
              This.setData({
                historyData: This.data.historyData.concat(res.data.data),
                pageNum: This.data.pageNum + 1,
              });
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            });
            This.setData({
              historyData: []
            });
          },
        });
      },
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})